package com.vg.view;

import java.util.ArrayList;

import kds.pulltorefesh.PullToRefreshBase;
import kds.pulltorefesh.PullToRefreshBase.OnLastItemVisibleListener;
import kds.pulltorefesh.PullToRefreshBase.OnRefreshListener;
import kds.pulltorefesh.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vg.com.chatonline.activity.R;
import vg.com.chatonline.adapter.adapter_yahoo;
import vg.com.classdata.yahoo;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import ce51.dunghn.imagethread.JsonParse;

import com.kdstudio.network.URLCachePolicy;
import com.kdstudio.network.URLRequest;
import com.kdstudio.network.URLRequestDelegate;
import com.kdstudio.network.reponse.URLJSONReponse;

public class Update_Status extends Fragment {
	ArrayList<yahoo> arr;
	adapter_yahoo adapter;
	private ListView lv;
	private int currentPage = 0;
	private int previousTotal = 0;
	private boolean loading = true;
	PullToRefreshListView mPtr;
	String url = "";
	 Handler mHandler;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.listview_ya, null);
		mPtr = (PullToRefreshListView) v.findViewById(R.id.listview);
		lv = mPtr.getRefreshableView();
		mHandler = new Handler();

		arr = new ArrayList<yahoo>();
		url = "http://deduocyeu.com/json/getUserStatus.php?page=" + currentPage
				+ "&pagesize=10";

		URLRequest request = new URLRequest(url, new RequestDelegate());
		request.setReponse(new URLJSONReponse());
		request.setCachePolicy(URLCachePolicy.LOCAL);
		request.send();
		return v;
	}

	public static Fragment newinstance() {
		return new Update_Status();

	}
	
	private class RequestDelegate extends URLRequestDelegate {

		@Override
		public void onRequestReponse(final URLRequest httpRequest) {
			mHandler.post(new Runnable() {

				public void run() {
					// TODO Auto-generated method stub
					// URLBitmapResponse
					// reponse=(URLBitmapResponse)httpRequest.getReponse();
					// img.setImageBitmap(reponse.bitmap);

					URLJSONReponse response = (URLJSONReponse) httpRequest
							.getReponse();
					
					JSONArray jarr;
					try {
						jarr = response.jsonObj.getJSONArray("data");
						for (int i = 0; i < 20; i++) {
							JSONObject obj = jarr.getJSONObject(i);
								String name_test = obj.getString("use_fullname");
								String Status_test = obj.getString("use_status");
								String img = obj.getString("use_avatar");
							yahoo yahoo = new yahoo(Status_test,name_test,img,0);
							arr.add(yahoo);
							adapter = new adapter_yahoo(getActivity(), arr);
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
					lv.setAdapter(adapter);
					adapter.notifyDataSetChanged();
					// tv.setText(response.jsonObj.toString());

				}
			});

		}
	}

}
